package cn.ncupyh.comptest;

//交换两个整型数组
public class SwapIntArray {
    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3, 4};
        int[] arr2 = {4, 5, 6, 7, 8};
        swap(arr1, arr2);
    }

    private static void swap(int[] arr1, int[] arr2) {
        int[] tmp1 = new int[arr2.length];
        int[] tmp2 = new int[arr1.length];
        for (int i = 0; i < arr2.length; i++) {//将arr2的数组内容复制到tmp1
            tmp1[i] = arr2[i];
        }
        for (int i = 0; i < arr1.length; i++) {//将arr1的数组内容复制到tmp2
            tmp2[i] = arr1[i];
        }
        arr1 = tmp1.clone();
        arr2 = tmp2.clone();
        System.out.println(java.util.Arrays.toString(arr1));
        System.out.println(java.util.Arrays.toString(arr2));
    }
}
